﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine.Draw;

namespace Etapa1.SteeringExperiments
{
    public class Separation : Steering
    {
        IWorld world;
        float rayOfAction = 500;
        List<IPhysicObject> collisionList = null;

        public List<IPhysicObject> CollisionList
        {
            get { return collisionList; }
            set { collisionList = value; rayOfAction = 0; }
        }

        public float RayOfAction
        {
            get { return rayOfAction; }
            set { rayOfAction = value; }
        }
        public Separation(IWorld world) :base(BehaviorType.separation)
        {
            this.world = world;
        }

        public override Microsoft.Xna.Framework.Vector3 CalculateSteering(SteeringEntityInfo entity)
        {            
            Vector3 steering = Vector3.Zero;
            List<IPhysicObject> list = null;
            if (rayOfAction <= 0)
            {
                list = collisionList;
            }
            else
            {
                list = world.PhysicWorld.GetPhysicsObjectsInRange(entity.Obj.PhysicObject, rayOfAction, (a, b) => true);
            }
            //int numAdded = 0;
            foreach (IPhysicObject obj in list)
            {
                if (obj != entity.Obj.PhysicObject && obj.PhysicObjectTypes != PhysicObjectTypes.TRIANGLEMESHOBJECT)
                {
                    Vector3 toAgent = entity.Obj.Position - obj.Position;
                    float tdist = toAgent.Length();
                    toAgent.Normalize();
                    steering += (toAgent / tdist) ;
                }
            }

            //if (numAdded > 0)
            //{
            //    //steering /= numAdded;                
            //}


            //steering.Normalize();
            //steering *= entity.MaxVelocity;

            Drawing.Draw3dLine(entity.Obj.Position,entity.Obj.Position + steering,Color.Silver);
            Drawing.DrawCircleOrDisk(rayOfAction, Vector3.Up, entity.Obj.Position, Color.Silver, 100, false, true);

            return steering ;

        }
    }
}
